import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV


def defaultLR(train):
    # 使用默认的LogisticRegression模型训练方法
    y_train = train['Target']
    X_train = train.drop('Target', axis=1)
    feature_names = X_train.columns
    lr = LogisticRegression()
    # 负log似然损失
    score_log = cross_val_score(lr, X_train, y_train, scoring='neg_log_loss', cv=5)
    print('score of each fold is: ', -score_log)
    print('the logloss of default LogisticRegression is: ', -score_log.mean())
    # 正确率
    score_acc = cross_val_score(lr, X_train, y_train, scoring='accuracy', cv=5)
    print('score of each fold is: ', score_acc)
    print('the accuracy default of LogisticRegression is: ', score_acc.mean())


def refineLR(train):
    # GridSearchCv, LogisticRegressionCV 任意使用一种即可
    y_train = train['Target']
    X_train = train.drop(['Target'], axis=1)
    feature_names = X_train.columns
    # 设置超参数范围
    # penalties = ['l1', 'l2']
    # Cs = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
    parameters = {'penalty': ['l1', 'l2'], 'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000]}
    lr = LogisticRegression(solver='liblinear')

    # 负log似然损失
    print("using 'neg_log_loss' as scoring parameter:")
    grid_log = GridSearchCV(lr, parameters, 'neg_log_loss', cv=5, return_train_score=True)
    grid_log.fit(X_train, y_train)
    print('the best score of GridSearchCV is: ', -grid_log.best_score_)
    print('the best parameter of GridSearchCV is: ', grid_log.best_params_)
    # 每个feature的系数
    df_log = pd.DataFrame({'columns': list(feature_names), 'coefficient': list(grid_log.best_estimator_.coef_.T)})
    print(df_log.sort_values(by=['coefficient'], ascending=False))

    # 正确率
    print("using 'accuracy' as scoring parameter:")
    grid_acc = GridSearchCV(lr, parameters, 'accuracy', cv=5, return_train_score=True)
    grid_acc.fit(X_train, y_train)
    print('the best score of GridSearchCV is: ', grid_acc.best_score_)
    print('the best parameter of GridSearchCV is: ', grid_acc.best_params_)
    # 每个feature的系数
    df_acc = pd.DataFrame({'columns': list(feature_names), 'coefficient': list(grid_acc.best_estimator_.coef_.T)})
    print(df_acc.sort_values(['coefficient'], ascending=False))


if __name__ == '__main__':
    train = pd.read_csv('pima-indians-diabetes_FE.csv')
    # defaultLR(train)
    refineLR(train)
